<?php
class Cart extends CI_Controller
{
    function __construct()
    {
        parent ::__construct();
        session_start();
        $this->load->model('category_model');
        $this->load->model('product_model');
        $this->load->model('order_model');
        $this->load->helper('main_helper');
        //$this->output->enable_profiler();
    }

    function add_cart($product_id)
    {

        $this->load->library('cart');
        $product = $this->product_model->getOne($product_id);
        //echo '<pre>'; var_dump($product); echo '</pre>';die;
        //tang quantily
        foreach ($this->cart->contents() as $rowid => $cart_item) {
            if ($product_id == $cart_item['id']) {
                $item = $cart_item;
                break;
            }
        }
        //echo '<pre>'; var_dump($item); echo '</pre>';die;
        if (isset($item)) {
            // update cart
            $data = array(
                'rowid' => $item['rowid'],
                'qty' => $item['qty'] + 1
            );
            //echo '<pre>'; var_dump($data); echo '</pre>';die;
            $this->cart->update($data);
        } else {
            //echo '<pre>'; var_dump($product); echo '</pre>';die;
            //insert cart
            $data = array(
                'id' => $product->product_id,
                'qty' => 1,
                'price' => $product->product_price,
                'name' => $product->product_name,
                'image' => $product->product_image,
                'product_shipping' => $product->product_shipping,
                'options' => array()
            );
            //echo '<pre>'; var_dump($data); echo '</pre>';die;

            $this->cart->insert($data);

        }
        redirect('cart/show_cart');
    }

    function show_cart()
    {
        $data['cart'] = $this->cart->contents();
        $data['sub_all'] = $this->cart->total();
        $data['total_shipping'] = $this->cart->total_shipping();
        $data['all_categories'] = $this->category_model->getAll();

        if ($data['post'] = $this->input->post()) {
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //them the div vao loi
            $this->form_validation->set_rules($this->order_model->getRules());
            if ($this->form_validation->run() === true) {
                $data['carts'] = $this->cart->contents();
                // echo '<pre>'; var_dump($data['cart']); echo '</pre>';die;
                foreach ($data['carts'] as $cart) {
                    $data = array(
                        'order_name' => $this->input->post('order_name'),
                        'order_email' => $this->input->post('order_email'),
                        'order_phone_no' => $this->input->post('order_phone_no'),
                        'order_deliver_method' => $this->input->post('order_deliver_method'),
                        'order_address' => $this->input->post('order_address'),
                        'order_comment' => $this->input->post('order_comment'),
                        'order_product_name' => $cart['name'],
                        'order_product_price' => $cart['price'],
                        'order_quantity' => $cart['qty'],
                        'order_product_id' => $cart['id'],
                        'order_product_shipping' => $cart['product_shipping'],
                        'order_total_cost' => $cart['subtotal']

                    );
                    //echo '<pre>'; var_dump($data); echo '</pre>';die;
                    $this->order_model->save($data);
                }
                $this->session->set_userdata('order_name', $this->input->post('order_name'));
                $this->session->set_userdata('order_email', $this->input->post('order_email'));
                $this->session->set_userdata('order_phone_no', $this->input->post('order_phone_no'));
                $this->session->set_userdata('order_deliver_method', $this->input->post('order_deliver_method'));
                $this->session->set_userdata('order_address', $this->input->post('order_address'));
                $this->session->set_userdata('order_comment', $this->input->post('order_comment'));
                redirect('cart/donhang');
            }
        }
        $this->load->view('fontend/product/show_cart', $data);

    }

    function donhang()
    {
        $data['carts'] = $this->cart->contents();
        $data['total_all'] = $this->cart->total();
        $data['total_shipping'] = $this->cart->total_shipping();
        $this->cart->destroy();

        $this->load->view('fontend/product/donhang', $data);
    }

    function ajax_update()
    {
        $rowid = $this->input->post('rowid');

        $data = array(
            'rowid' => $rowid,
            'qty' => $this->input->post('quantity')
        );

        $this->cart->update($data);
        $return = array(
            'status' => true,
            'subtotal' => $this->cart->_cart_contents[$rowid]['subtotal'],
            'total_current' => $this->cart->total()
        );
        //echo '<pre>'; var_dump(json_encode($return)); echo '</pre>';die;
        echo json_encode($return);
    }

    function destroy()
    {
        $this->cart->destroy();
        redirect('cart/show_cart');
    }
}

